/*
	* --------------------------------------------------------------------------
	*
  * Copyright 2011 Matt West
	*
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
	*
  *   http://www.apache.org/licenses/LICENSE-2.0
	*
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  * --------------------------------------------------------------------------
*/


/*
*
*/
function loadProjects() {
	
	var projectsArray = localStorage['projects'];
	
	if (projectsArray == undefined || projectsArray.length == 2) {
		
		$('.showOptions').live('click', function() {
			chrome.tabs.create({ 'url': 'options.html' });
		});
		
		var html = '<div class="noProject">You have no projects!<br/>Add some on the <a href="#" class="showOptions">options page</a></div>';
		
		document.getElementById('container').innerHTML = html;
	} else {
		
		var projects = JSON.parse(projectsArray);
		
		showProjectsList();
		
	}
	
	var lastUpdated = localStorage['lastUpdated'];
	
	if (lastUpdated != undefined) {
		var d = new Date(lastUpdated);
		document.getElementById('lastUpdated').innerHTML = 'Last Updated: ' + d.toLocaleTimeString();
	}
}


/*
*	Show the list of projects
* 
* @param	projects	An array of projects
*/
function showProjectsList() {
	
	var projects = JSON.parse(localStorage['projects']);
	
	// Insert the list
	document.getElementById('container').innerHTML = '<ul id="projectsList"></ul>';
	
	for (var i = 0; i < projects.length; i++) {
	
		projectName = projects[i][0];
		numIssues = projects[i][1].length;
		
		if (numIssues == 0) {
			colorClass = "good";
		} else {
			colorClass = "bad";
		}
		
		document.getElementById('projectsList').innerHTML += '<li><a href="#" onclick="showIssues('+i+')">'+projectName+'</a><div class="newIssues '+colorClass+'">'+numIssues+'</a></li>';
		
	}
	
	document.getElementById('back').innerHTML = '';
}


/*
*	Show issues for a project
*
* @param	position	The position of this project in the projects array
*/
function showIssues(position) {

	// Insert the list
	document.getElementById('container').innerHTML = '<ul id="projectsList"></ul>';
	
	var projects = JSON.parse(localStorage['projects']);
	var project = projects[position];
	var projectTitle = project[0];
	var issues = project[1];
	
	document.getElementById('header').innerHTML = projectTitle + ' issues';
	
	// Clear the list
	var listView = document.getElementById('projectsList');
	listView.innerHTML = '';
	
	if (issues.length == 0) {
	
		listView.innerHTML = '<li class="noIssues">Awesome! There are no new issues for this project.<br/><a href="#" onclick="showProjectsList()">Go Back</a></li>'
		
	} else {
		// Display each issue
		for (var i = 0; i < issues.length; i++) {
		
			var issue = issues[i];
			var id = issue[0];
			var title = issue[1];
			var author = issue[2];
		
			var html = '<li><a href="#" onclick="showIssue('+position+', '+i+')" class="issueItem">'+title+'<br/><span class="issueAuthor">'+author+'</span></a></li>';
		
			listView.innerHTML += html;
		}
	}
	
	document.getElementById('back').innerHTML = '<a href="#" onclick="showProjectsList()">Go Back</a>';
	
}


/*
*	Show an issues
*/
function showIssue(projectPosition, issuePosition) {
	
	var projects = JSON.parse(localStorage['projects']);
	var project = projects[projectPosition];
	
	var projectTitle = project[0];
	var issues = project[1];
	
	var issue = issues[issuePosition];
	
	var id = issue[0];
	var title = issue[1];
	var author = issue[2];
	var content = issue[3];
	var published = issue[4];
	
	// Preserve new lines as innerHTML will kill them
	var processedContent = content.replace(/\n/g, "<br/>");
	
	var d = new Date(published);
	var date = d.toDateString();
	
	var url = "'"+id+"'"; // Ugly but it works
	
	var html = '<div id="issue"><div id="iTitle">'+title+'</div><div id="iAuthor">'+author+'</div><div id="iContent">'+processedContent+'</div><div id="iDate">Published: '+date+'</div><div id="iLink"><a href="#" onclick="launchIssue('+url+')">View Issue on Google Code</a></div></div>';

	document.getElementById('header').innerHTML = projectTitle + ' issues';
	document.getElementById('container').innerHTML = html;
	
	document.getElementById('back').innerHTML = '<a href="#" onclick="showIssues('+projectPosition+')">Go Back</a>';
	
}


/*
*
*/
function launchIssue(url) {
	chrome.tabs.create({ 'url': url });
}
